/**
 * 
 */
package net.xfok.dict;

import android.util.Log;

/**
 * @author user
 *
 */
public class SubString {
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if(isChinese(c))return true;
			}
			return false;	
		}

		public static boolean isChinese(char c) {
			Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
			if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
					|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
					|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
					|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
					|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
				return true;
			}
			return false;
		}
	
	
	
	/**
	 * ��ȡ�����ַ�
	 * @param str
	 * @param len
	 * @param Character_Encoding
	 * @return
	 */
	public static String getLimitLengthString( String str,int len,String Character_Encoding){ 
		try{ 
		int counterOfDoubleByte = 0; 
		byte[] b = str.getBytes(Character_Encoding); 
		if(b.length <= len) 
		return str; 
		for(int i = 0; i < len; i++){
		if(b[i] < 0) 
		counterOfDoubleByte++; 
		} 
		if(counterOfDoubleByte % 2 == 0) 
		return new String(b, 0, len, Character_Encoding); 
		else 
		return new String(b, 0, len - 1, Character_Encoding); 
		}catch(Exception ex){ 
		Log.e("SubString", ex.getMessage()); 
		return ""; 
		}

	}
	
	
	public static String getLimitLengthStringAndInertStr(String str,int len,String inertStr,String Character_Encoding){ 
		try{ 
		int counterOfDoubleByte = 0; 
		len = len-inertStr.length();
		byte[] b = str.getBytes(Character_Encoding); 
		if(b.length <= len) 
			{
			for(int i=0;i<len-b.length;i++)str=str+" ";
			return str+inertStr;
			}
		 
		for(int i = 0; i < len; i++){
		if(b[i] < 0) 
		counterOfDoubleByte++; 
		} 
		if(counterOfDoubleByte % 2 == 0) 
		return new String(b, 0, len, Character_Encoding)+inertStr; 
		else 
		return new String(b, 0, len - 1, Character_Encoding); 
		}catch(Exception ex){ 
		Log.e("SubString", ex.getMessage()); 
		return str+" "+inertStr; 
		}

	}

	
	
	
	
	
	/**
     * �ַ��ֽڽ�ȡ
     * @param str ԭ�ַ�
     * @param len ��ȡ����
     * @return String
     * @author LEO
     */

     public static String splitString(String str, int len) {
            return splitString(str, len, "...");

     }

     /**
      * �ַ��ֽڽ�ȡ
      * @param str ԭ�ַ�
      * @param len ��ȡ����
      * @param elide ʡ�Է�
      * @return String
      */

      public static String splitString(String str,int len,String elide) {
             if (str == null) {
                    return "";
             }
             byte[] strByte = str.getBytes();
             int strLen = strByte.length;
             int elideLen = (elide.trim().length() == 0) ? 0 : elide.getBytes().length;
             if (len >= strLen || len < 1) {
                    return str;
             }
             if (len - elideLen > 0) {
                    len = len - elideLen;
             }
             int count = 0;
             for (int i = 0; i < len; i++) {
                    int value = (int) strByte[i];
                    if (value < 0) {
                           count++;
                    }
             }
             if (count % 2 != 0) {
                    len = (len == 1) ? len + 1 : len - 1;
             }
             return new String(strByte, 0, len) + elide.trim();
      }
}
